package com.jeesite.modules.config.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.jeesite.modules.goods.entity.Brand;
import com.jeesite.modules.goods.entity.GoodsType;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.service.BrandService;
import com.jeesite.modules.goods.service.GoodsTypeService;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.jdProduct.utils.ProductPoolUtils;

/**
 * @author lnj
 * createTime 2018-11-07 22:25
 **/
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
	@Autowired
	ZtGoodsService ztGoodsService;
	@Autowired
	GoodsTypeService goodsTypeService;
	@Autowired
	BrandService brandService;
    @Override
    public void run(String... args) throws Exception {
     //  s();
    }
    public void s(){
    	List<ZtGoods> plp=ztGoodsService.findList(new ZtGoods());
		List<ZtGoods> plpList=new ArrayList<ZtGoods>();
		List<Brand> brandList=new ArrayList<Brand>();
		for(ZtGoods pp:plp){
			long sku=Long.valueOf(pp.getCode());
			
			ZtGoods ztGoods=ProductPoolUtils.productDetail(sku);
			if(ztGoods==null){
				continue;
			}
			ztGoods.setGoodspool(pp.getPageNum()+"");
			ztGoods.setId(pp.getId());
			
			String type=ztGoods.getType();
			String typestr="";
			String typeName="";
			if(StringUtils.isNotBlank(type)){
				String[] typearr=type.split(";");
				if(typearr!=null && typearr.length>0){
					for(String ty:typearr){
						GoodsType goodsType=new GoodsType();
						goodsType.setGoodsCode(ty);
						goodsType=goodsTypeService.get(goodsType);
						if(goodsType!=null){
							typestr+=ty+",";
							typeName+=goodsType.getName()+",";
						}
					}
					if(StringUtils.isNotBlank(typestr)&& typestr.length()>1){
	 					typestr=typestr.substring(0,typestr.length()-1);
	 				}
	 				if(StringUtils.isNotBlank(typeName)&& typeName.length()>1){
	 					typeName=typeName.substring(0,typeName.length()-1);
	 				}
				}
			}
			
			ztGoods.setType(typestr);
			ztGoods.setTypeName(typeName);
			
			Brand brand=new Brand();
			
			String bname=ztGoods.getBrandName();
			
			brand.setName(bname.trim());
			Brand ba=brandService.getByName(brand);
			if(ba==null || StringUtils.isBlank(ba.getId())){
				brand.setIsshow("1");
				brand.setCreateDate(new Date());
				brand.setType(typestr);
				brand.setTypeName(typeName);
				brandService.save(brand);
				ztGoods.setBrandid(brand.getId());

			}else{
				ztGoods.setBrandid(ba.getId());
			}
			
			plpList.add(ztGoods);
			
			if((plpList.size()%300)==0){
				ztGoodsService.batchUpdate(plpList);
				
				plpList.clear();
			}
			
		}
		
		if(plpList!=null && plpList.size()>0){
			ztGoodsService.batchUpdate(plpList);
		}
		
		if(brandList!=null && brandList.size()>0){
			brandService.batchSave(brandList);
		}
    }
}
